<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="orderNo">
        <el-input v-model="queryParams.orderNo" placeholder="请输入订单编号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="订单状态" prop="payStats" style="width: 240px">
        <el-select v-model="queryParams.payStats" placeholder="请选择">
          <el-option v-for="dict in order_status" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="圈子名称" prop="circleId" style="width: 240px">
        <el-select v-model="queryParams.circleId" placeholder="请选择">
          <el-option v-for="dict in circleList" :key="dict.id" :label="dict.name" :value="dict.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['order:order:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['order:order:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['order:order:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['order:order:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <!-- <el-table-column label="id" align="center" prop="id" /> -->
      <el-table-column label="订单号" align="center" prop="orderNo" width="245" />
      <el-table-column label="交单单号" align="center" prop="tranNo" width="245" />
      <!-- <el-table-column label="师傅id" align="center" prop="masterId" /> -->
      <!-- <el-table-column
        label="面料图片"
        align="center"
        prop="images"
        width="100"
      >
        <template #default="scope">
          <image-preview :src="scope.row.images" :width="50" :height="50" />
        </template>
</el-table-column> -->
      <!-- <el-table-column label="标题" align="center" prop="title" /> 
      <el-table-column label="需求说明" align="center" prop="summany">
        <template #default="scope">
          
          <div
            v-if="scope.row.summany != null && scope.row.summany.length <= 10"
          >
            {{ scope.row.summany }}
          </div>
        
          <el-tooltip
            v-if="scope.row.summany != null && scope.row.summany.length > 10"
            popper-class="testtooltip"
            :content="scope.row.summany"
            placement="top-start"
          >
            <div>{{ scope.row.summany.slice(0, 10) }}...</div>
          </el-tooltip>
        </template>
      </el-table-column> -->
      <el-table-column label="创建时间" align="center" prop="createTime" width="200" />
      <el-table-column label="圈子名称" align="center" prop="circleName" />
      <el-table-column label="创建人" align="center" prop="createName" />
      <el-table-column label="创建人id" align="center" prop="createUser" width="150">
      </el-table-column>
      <el-table-column label="金额" align="center" prop="money" />
      <!-- <el-table-column label="物流编号" align="center" prop="logisticsNo" />
      <el-table-column label="快递公司" align="center" prop="comNo" /> -->
      <!-- <el-table-column label="总价" align="center" prop="totalPrice" /> -->
      <!-- <el-table-column label="备注" align="center" prop="memo" /> -->
      <!-- (0待接单 1已接单 2制作中 3待收货 4已完成 5已取消) -->
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <span>{{ scope.row.payStats == 0 ? "未支付" : scope.row.payStats == 1 ? "已支付" : "已退款" }}</span>
          <!-- <dict-tag :options="order_status" :value="scope.row.payStats" /> -->
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template #default="scope">
          <el-button link type="primary" icon="Expand" @click="checkInfoChange(scope.row)"
            v-hasPermi="['order:order:info']">查看详情</el-button>
          <el-button link type="primary" icon="Edit" v-if="
            scope.row.payStats === 1
          " @click="handleRefund(scope.row)" v-hasPermi="['order:order:edit']">退款</el-button>
          <!-- <el-button
            link
            type="primary"
            icon="Edit"
            v-if="
              scope.row.status === 0 &&
              scope.row.isSame === 1 &&
              (!scope.row.takingIds || scope.row.takingIds.length === 0)
            "
            @click="handleUpdate(scope.row)"
            v-hasPermi="['order:order:edit']"
            >指定接单</el-button
          > -->

          <!-- <el-button
            link
            type="primary"
            icon="Edit"
            v-if="scope.row.status < 5"
            @click="handleShouHou(scope.row)"
            v-hasPermi="['order:order:edit']"
            >指定售后</el-button
          > 
          <el-button
            link
            type="primary"
            icon="Edit"
            v-if="
              scope.row.status !== 0 &&
              scope.row.status !== 4 &&
              scope.row.status !== 6
            "
            @click="handleRefund(scope.row)"
            v-hasPermi="['order:order:edit']"
            >退款</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['order:order:remove']"
            >删除</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改订单对话框 -->
    <el-dialog :title="title" v-model="open1" width="900px" append-to-body>
      <el-form ref="orderRef" :model="form" :rules="rules" label-width="130px">
        <el-form :model="queryParams1" ref="queryRef1" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="师傅姓名" prop="nickName">
            <el-input v-model="queryParams1.nickName" placeholder="请输入师傅姓名" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="联系方式" prop="phonenumber">
            <el-input v-model="queryParams1.phonenumber" placeholder="请输入联系方式" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery1">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery1">重置</el-button>
          </el-form-item>
        </el-form>
        <el-table v-loading="loading" :data="masterList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="头像" align="center" prop="avatar">
            <template #default="scope">
              <image-preview :src="scope.row.avatar" :width="50" :height="50" />
            </template>
          </el-table-column>
          <!-- <el-table-column label="师傅名称" align="center" prop="nickName" />
          <el-table-column label="师傅等级" align="center" prop="levelName" />
          <el-table-column label="接单次数" align="center" prop="serviceNum" /> -->
        </el-table>
        <pagination v-show="total1 > 0" :total="total1" v-model:page="queryParams1.pageNum"
          v-model:limit="queryParams1.pageSize" @pagination="getMasterList" />
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog style="border-radius: 8px" v-model="dialogTableVisible" title="订单详情" width="800">
      <div class="grid-data">
        <div class="grid-data-content">
          <div class="grid-data-list">
            <div class="grid-header-text">
              <span>订单号</span>
              <span>{{ gridDataContentDesk?.orderNo }}</span>
            </div>
            <div class="grid-header-text">
              <span>交易单号</span>
              <span>{{ gridDataContentDesk?.tranNo }}</span>
            </div>
            <div class="grid-header-text">
              <span>创建时间</span>
              <span>{{ gridDataContentDesk?.getMasterList }}</span>
            </div>
            <div class="grid-header-text">
              <span>状态</span>
              <span>{{
                gridDataContentDesk?.payStats == 0 ? "未支付" : gridDataContentDesk?.payStats == 1 ? "已支付" : "已退款"
                }}</span>
            </div>
            <!-- <div class="grid-header-text">
              <span>面料图片</span>
              <ImagePreview1
                :src="gridDataContentDesk?.images"
                :width="50"
                :height="50"
              />
            </div> -->
            <div class="grid-header-text">
              <span>创建人</span>
              <span>{{ gridDataContentDesk?.createName }}</span>
            </div>
            <div class="grid-header-text">
              <span>创建人id</span>
              <span>{{ gridDataContentDesk?.createUser }}</span>
            </div>
            <!--<div class="grid-header-text">
              <span>需求说明</span>
              <span>{{ gridDataContentDesk?.summany }}</span>
            </div>
            <div class="grid-header-text">
              <span>开始时间</span>
              <span>{{ gridDataContentDesk?.beginTime }}</span>
            </div>
            <div class="grid-header-text">
              <span>寄出快递</span>
              <span v-if="gridDataContentDesk?.comName1 != null">{{
                gridDataContentDesk?.comName1 +
                ":" +
                gridDataContentDesk?.logisticsNo1
              }}</span>
            </div>
            <div class="grid-header-text">
              <span>应收快递</span>
              <span v-if="gridDataContentDesk?.comName != null">{{
                gridDataContentDesk?.comName +
                ":" +
                gridDataContentDesk?.logisticsNo
              }}</span>
            </div>

            <div class="grid-header-text">
              <span>用户名称</span>
              <span>{{ gridDataContentDesk?.createName }}</span>
            </div>
            <div class="grid-header-text">
              <span>接单师傅</span>
              <span>{{ gridDataContentDesk?.masterName }}</span>
            </div> -->
            <div class="grid-header-text">
              <span>金额</span>
              <span>{{ gridDataContentDesk?.money }}</span>
            </div>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup name="Order">
import {
  listOrder,
  getOrder,
  delOrder,
  addOrder,
  updateOrder,
  refundOrder,
  getCircleList,
} from "@/api/main/order";
import { listMaster } from "@/api/main/master";
import ImagePreview1 from "@/components/ImagePreview1/index.vue";

const { proxy } = getCurrentInstance();
const order_status = reactive([
  { label: "未支付", value: 0 },
  { label: "已支付", value: 1 },
  { label: "已退款", value: 3 },
]);
const route = useRoute();
const orderList = ref([]);
const open = ref(false);
const open1 = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const total1 = ref(0);
const title = ref("");
const dialogTableVisible = ref(false);
const gridDataContentDesk = ref();
const masterList = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderNo: null,
    masterId: null,
    images: null,
    title: null,
    summany: null,
    budget: null,
    type: null,
    takeTime: null,
    beginTime: null,
    buyNo: null,
    logisticsNo: null,
    addrId: null,
    comNo: null,
    totalPrice: null,
    memo: null,
    status: null,
    createUser: null,
    createName: null,
    updateUser: null,
    updateName: null,
    circleId: null,
  },
  queryParams1: {
    pageNum: 1,
    pageSize: 10,
    serviceNum: null,
    publishNum: null,
    level: null,
    memo: null,
    status: null,
    createUser: null,
    createName: null,
    updateUser: null,
    updateName: null,
    nickName: null,
    userName: null,
    phonenumber: null,
    password: null,
  },
  rules: {
    status: [
      {
        required: true,
        message:
          "状态(0待接单 1已接单 2制作中 3待收货 4已完成 5已取消)不能为空",
        trigger: "change",
      },
    ],
    delFlag: [
      { required: true, message: "是否删除;0否1是不能为空", trigger: "blur" },
    ],
  },
});

const { queryParams, queryParams1, form, rules } = toRefs(data);
const circleList = ref([]);
const getCircle = async () => {
  const res = await getCircleList();
  if (res.code == 200) {
    circleList.value = res.data;
  }
  console.log(res);
  // return res;
};

/** 查询订单列表 */
function getList(value) {
  //检查params.id是否存在且不为空
  if (value.id) {
    queryParams.value.createUser = value.id;
  }
  loading.value = true;
  listOrder(queryParams.value).then((response) => {
    orderList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
function getMasterList() {
  listMaster(queryParams1.value).then((response) => {
    masterList.value = response.rows;
    total1.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  open1.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    orderNo: null,
    masterId: null,
    images: null,
    title: null,
    summany: null,
    budget: null,
    type: null,
    takeTime: null,
    beginTime: null,
    preMasterId: [],
    buyNo: null,
    logisticsNo: null,
    addrId: null,
    comNo: null,
    totalPrice: null,
    memo: null,
    status: null,
    delFlag: null,
    createUser: null,
    createName: null,
    createTime: null,
    updateUser: null,
    updateName: null,
    updateTime: null,
    circleId: null,
  };
  proxy.resetForm("orderRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList(route.params);
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 搜索按钮操作 */
function handleQuery1() {
  queryParams1.value.pageNum = 1;
  getMasterList();
}

/** 重置按钮操作 */
function resetQuery1() {
  proxy.resetForm("queryRef1");
  handleQuery1();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.userId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加订单";
}
/** 指定接单按钮操作 */
function handleTaking(row) {
  reset();
  form.value.id = row.id;
  open1.value = true;
  title.value = "指定接单师傅";
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getOrder(_id).then((response) => {
    form.value = response.data;
    open1.value = true;
    title.value = "指定接单师傅";
  });
}
/** 指定售后按钮操作 */
function handleShouHou(row) {
  proxy.$modal
    .confirm('是否确认指定订单编号为"' + row.orderNo + '"的数据项为售后中吗？')
    .then(function () {
      return updateOrder({ id: row.id, status: 5 });
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("操作成功");
    })
    .catch(() => { });
}
//退款
function handleRefund(row) {
  proxy.$modal
    .confirm('是否确认指定订单编号为"' + row.orderNo + '"的数据项退款吗？')
    .then(async function () {
      const res = await refundOrder(row.id);
      if (res.code == 200) {
        proxy.$modal.msgSuccess(res.msg);
        getList();
      } else {
        proxy.$modal.msgError(res.msg);
      }
    })
    .then(() => {

    })
    .catch(() => { });
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["orderRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        form.value.preMasterId = ids.value;
        updateOrder(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open1.value = false;
          getList(route.params);
        });
      } else {
        addOrder(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList(route.params);
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除订单编号为"' + row.orderNo + '"的数据项？')
    .then(function () {
      return delOrder(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "order/order/export",
    {
      ...queryParams.value,
    },
    `order_${new Date().getTime()}.xlsx`
  );
}

getList(route.params);
getCircle();
// 查看详情
const checkInfoChange = (parms) => {
  dialogTableVisible.value = !dialogTableVisible.value;
  getOrder(parms.id).then(async (res) => {
    gridDataContentDesk.value = res.data;
  });
};
</script>
<style lang="scss" scoped>
.grid-data-list {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;

  .grid-header-text {
    height: 60px;
    width: 50%;
    display: flex;
    align-items: center;
    border: 1px solid #eee;

    img {
      width: 50px;
      height: 50px;
    }

    &:nth-child(3n) {
      background: #fafafa;
    }

    span {
      &:nth-child(1) {
        width: 25% !important;
        display: block;
        padding-left: 20px;
      }
    }
  }
}
</style>
